home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / macpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  12.0 KB  |  322 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Pathname and path-related operations for the Macintosh.'''
  5. import os
  6. from stat import *
  7. __all__ = [
  8.     'normcase',
  9.     'isabs',
  10.     'join',
  11.     'splitdrive',
  12.     'split',
  13.     'splitext',
  14.     'basename',
  15.     'dirname',
  16.     'commonprefix',
  17.     'getsize',
  18.     'getmtime',
  19.     'getatime',
  20.     'getctime',
  21.     'islink',
  22.     'exists',
  23.     'isdir',
  24.     'isfile',
  25.     'walk',
  26.     'expanduser',
  27.     'expandvars',
  28.     'normpath',
  29.     'abspath',
  30.     'curdir',
  31.     'pardir',
  32.     'sep',
  33.     'pathsep',
  34.     'defpath',
  35.     'altsep',
  36.     'extsep',
  37.     'realpath',
  38.     'supports_unicode_filenames']
  39. curdir = ':'
  40. pardir = '::'
  41. extsep = '.'
  42. sep = ':'
  43. pathsep = '\n'
  44. defpath = ':'
  45. altsep = None
  46.  
  47. def normcase(path):
  48.     return path.lower()
  49.  
  50.  
  51. def isabs(s):
  52.     '''Return true if a path is absolute.
  53.     On the Mac, relative paths begin with a colon,
  54.     but as a special case, paths with no colons at all are also relative.
  55.     Anything else is absolute (the string up to the first colon is the
  56.     volume name).'''
  57.     if ':' in s:
  58.         pass
  59.     return s[0] != ':'
  60.  
  61.  
  62. def join(s, *p):
  63.     path = s
  64.     for t in p:
  65.         if not s or isabs(t):
  66.             path = t
  67.             continue
  68.         
  69.         if t[:1] == ':':
  70.             t = t[1:]
  71.         
  72.         if ':' not in path:
  73.             path = ':' + path
  74.         
  75.         if path[-1:] != ':':
  76.             path = path + ':'
  77.         
  78.         path = path + t
  79.     
  80.     return path
  81.  
  82.  
  83. def split(s):
  84.     '''Split a pathname into two parts: the directory leading up to the final
  85.     bit, and the basename (the filename, without colons, in that directory).
  86.     The result (s, t) is such that join(s, t) yields the original argument.'''
  87.     if ':' not in s:
  88.         return ('', s)
  89.     
  90.     colon = 0
  91.     for i in range(len(s)):
  92.         if s[i] == ':':
  93.             colon = i + 1
  94.             continue
  95.     
  96.     (path, file) = (s[:colon - 1], s[colon:])
  97.     if path and not (':' in path):
  98.         path = path + ':'
  99.     
  100.     return (path, file)
  101.  
  102.  
  103. def splitext(p):
  104.     '''Split a path into root and extension.
  105.     The extension is everything starting at the last dot in the last
  106.     pathname component; the root is everything before that.
  107.     It is always true that root + ext == p.'''
  108.     i = p.rfind('.')
  109.     if i <= p.rfind(':'):
  110.         return (p, '')
  111.     else:
  112.         return (p[:i], p[i:])
  113.  
  114.  
  115. def splitdrive(p):
  116.     """Split a pathname into a drive specification and the rest of the
  117.     path.  Useful on DOS/Windows/NT; on the Mac, the drive is always
  118.     empty (don't use the volume name -- it doesn't have the same
  119.     syntactic and semantic oddities as DOS drive letters, such as there
  120.     being a separate current directory per drive)."""
  121.     return ('', p)
  122.  
  123.  
  124. def dirname(s):
  125.     return split(s)[0]
  126.  
  127.  
  128. def basename(s):
  129.     return split(s)[1]
  130.  
  131.  
  132. def ismount(s):
  133.     if not isabs(s):
  134.         return False
  135.     
  136.     components = split(s)
  137.     if len(components) == 2:
  138.         pass
  139.     return components[1] == ''
  140.  
  141.  
  142. def isdir(s):
  143.     '''Return true if the pathname refers to an existing directory.'''
  144.     
  145.     try:
  146.         st = os.stat(s)
  147.     except os.error:
  148.         return 0
  149.  
  150.     return S_ISDIR(st.st_mode)
  151.  
  152.  
  153. def getsize(filename):
  154.     '''Return the size of a file, reported by os.stat().'''
  155.     return os.stat(filename).st_size
  156.  
  157.  
  158. def getmtime(filename):
  159.     '''Return the last modification time of a file, reported by os.stat().'''
  160.     return os.stat(filename).st_mtime
  161.  
  162.  
  163. def getatime(filename):
  164.     '''Return the last access time of a file, reported by os.stat().'''
  165.     return os.stat(filename).st_atime
  166.  
  167.  
  168. def islink(s):
  169.     '''Return true if the pathname refers to a symbolic link.'''
  170.     
  171.     try:
  172.         import Carbon.File as Carbon
  173.         return Carbon.File.ResolveAliasFile(s, 0)[2]
  174.     except:
  175.         return False
  176.  
  177.  
  178.  
  179. def isfile(s):
  180.     '''Return true if the pathname refers to an existing regular file.'''
  181.     
  182.     try:
  183.         st = os.stat(s)
  184.     except os.error:
  185.         return False
  186.  
  187.     return S_ISREG(st.st_mode)
  188.  
  189.  
  190. def getctime(filename):
  191.     '''Return the creation time of a file, reported by os.stat().'''
  192.     return os.stat(filename).st_ctime
  193.  
  194.  
  195. def exists(s):
  196.     '''Return True if the pathname refers to an existing file or directory.'''
  197.     
  198.     try:
  199.         st = os.stat(s)
  200.     except os.error:
  201.         return False
  202.  
  203.     return True
  204.  
  205.  
  206. def commonprefix(m):
  207.     '''Given a list of pathnames, returns the longest common leading component'''
  208.     if not m:
  209.         return ''
  210.     
  211.     prefix = m[0]
  212.     for item in m:
  213.         for i in range(len(prefix)):
  214.             if prefix[:i + 1] != item[:i + 1]:
  215.                 prefix = prefix[:i]
  216.                 if i == 0:
  217.                     return ''
  218.                 
  219.                 break
  220.                 continue
  221.         
  222.     
  223.     return prefix
  224.  
  225.  
  226. def expandvars(path):
  227.     '''Dummy to retain interface-compatibility with other operating systems.'''
  228.     return path
  229.  
  230.  
  231. def expanduser(path):
  232.     '''Dummy to retain interface-compatibility with other operating systems.'''
  233.     return path
  234.  
  235.  
  236. class norm_error(Exception):
  237.     '''Path cannot be normalized'''
  238.     pass
  239.  
  240.  
  241. def normpath(s):
  242.     '''Normalize a pathname.  Will return the same result for
  243.     equivalent paths.'''
  244.     if ':' not in s:
  245.         return ':' + s
  246.     
  247.     comps = s.split(':')
  248.     i = 1
  249.     while i < len(comps) - 1:
  250.         if comps[i] == '' and comps[i - 1] != '':
  251.             if i > 1:
  252.                 del comps[i - 1:i + 1]
  253.                 i = i - 1
  254.             else:
  255.                 raise norm_error, 'Cannot use :: immediately after volume name'
  256.         i > 1
  257.         i = i + 1
  258.     s = ':'.join(comps)
  259.     if s[-1] == ':' and len(comps) > 2 and s != ':' * len(s):
  260.         s = s[:-1]
  261.     
  262.     return s
  263.  
  264.  
  265. def walk(top, func, arg):
  266.     """Directory tree walk with callback function.
  267.  
  268.     For each directory in the directory tree rooted at top (including top
  269.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  270.     dirname is the name of the directory, and fnames a list of the names of
  271.     the files and subdirectories in dirname (excluding '.' and '..').  func
  272.     may modify the fnames list in-place (e.g. via del or slice assignment),
  273.     and walk will only recurse into the subdirectories whose names remain in
  274.     fnames; this can be used to implement a filter, or to impose a specific
  275.     order of visiting.  No semantics are defined for, or required of, arg,
  276.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  277.     a filename pattern, or a mutable object designed to accumulate
  278.     statistics.  Passing None for arg is common."""
  279.     
  280.     try:
  281.         names = os.listdir(top)
  282.     except os.error:
  283.         return None
  284.  
  285.     func(arg, top, names)
  286.     for name in names:
  287.         name = join(top, name)
  288.         if isdir(name) and not islink(name):
  289.             walk(name, func, arg)
  290.             continue
  291.     
  292.  
  293.  
  294. def abspath(path):
  295.     '''Return an absolute path.'''
  296.     if not isabs(path):
  297.         path = join(os.getcwd(), path)
  298.     
  299.     return normpath(path)
  300.  
  301.  
  302. def realpath(path):
  303.     path = abspath(path)
  304.     
  305.     try:
  306.         import Carbon.File as Carbon
  307.     except ImportError:
  308.         return path
  309.  
  310.     if not path:
  311.         return path
  312.     
  313.     components = path.split(':')
  314.     path = components[0] + ':'
  315.     for c in components[1:]:
  316.         path = join(path, c)
  317.         path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname()
  318.     
  319.     return path
  320.  
  321. supports_unicode_filenames = False
  322.